import os
from itemadapter import ItemAdapter

class SdweatherspiderPipeline(object):
    def __init__(self):
        # 定义文件路径，默认为当前目录下的 weather.txt
        self.file_path = 'weather.txt'

    def open_spider(self, spider):
        """
        在爬虫启动时触发，初始化或清理文件
        """
        # 检查并删除旧文件，避免内容重复
        if os.path.exists(self.file_path):
            try:
                os.remove(self.file_path)
                spider.logger.info(f"Successfully removed existing file: {self.file_path}")
            except Exception as e:
                spider.logger.error(f"Failed to remove file {self.file_path}: {e}")
        else:
            spider.logger.info(f"No existing file to remove: {self.file_path}")

    def process_item(self, item, spider):
        """
        处理每一个爬取到的 item，并将数据写入文件
        """
        # 使用 UTF-8 编码打开文件
        with open(self.file_path, 'a', encoding='utf-8') as fp:
            fp.write(item['city'] + '\n')          # 写入城市名
            fp.write(item['weather'] + '\n\n')     # 写入天气信息
        return item

    def close_spider(self, spider):
        """
        在爬虫结束时触发，执行收尾工作
        """
        spider.logger.info(f"Spider closed. Data saved to {self.file_path}")
